package com.fastjrun.share.demo.service.impl;


import com.fastjrun.apiworld.vo.PageResult;
import com.fastjrun.share.demo.dao.LogRecordDao;
import com.fastjrun.share.demo.dto.LogRecordQueryDTO;
import com.fastjrun.share.demo.helper.EntityToVOConverter;
import com.fastjrun.share.demo.mapper.LogRecordDynamicSqlSupport;
import com.fastjrun.share.demo.service.BaseDemoService;
import com.fastjrun.share.demo.service.LogRecordManagerService;
import com.fastjrun.share.demo.vo.LogRecordVO;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 注意：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目的
 *
 * @author cuiyingfeng @Copyright 2021 快嘉. All rights reserved.
 */
@Service("logRecordManagerService")
public class LogRecordManagerServiceImpl extends BaseDemoService
        implements LogRecordManagerService {
    @Resource
    LogRecordDao logRecordDao;

    @Override
    public PageResult<LogRecordVO> search(LogRecordQueryDTO requestBody) {

        Long userId = requestBody.getUserId();
        String mobile = requestBody.getMobile();
        String userName = requestBody.getUsername();
        String email = requestBody.getEmail();
        Integer current = requestBody.getCurrent();
        Integer size = requestBody.getSize();

        if (current == null || current == 0) {
            current = PAGE_CURRENT;
        }
        if (size == null || size == 0) {
            size = PAGE_SIZE;
        }

        int offset = (current - 1) * size;

        QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder builder = SqlBuilder.select(LogRecordDao.selectList)
                .from(LogRecordDynamicSqlSupport.logRecord)
                .where()
                .configureStatement(c -> c.setNonRenderingWhereClauseAllowed(true));

        if (userId != null) {
            builder.and(LogRecordDynamicSqlSupport.userId, SqlBuilder.isEqualTo(userId));
        }

        if (mobile != null && !mobile.equals("")) {
            builder.and(LogRecordDynamicSqlSupport.mobile, SqlBuilder.isEqualTo(mobile));

        }

        if (userName != null && !userName.equals("")) {
            builder.and(LogRecordDynamicSqlSupport.username, SqlBuilder.isLike(userName));
        }

        if (email != null && !email.equals("")) {
            builder.and(LogRecordDynamicSqlSupport.email, SqlBuilder.isEqualTo(email));

        }

        SelectStatementProvider selectStatement = builder
                .limit(size)
                .offset(offset)
                .build().render(RenderingStrategies.MYBATIS3);

        List<LogRecordVO> list =
                logRecordDao.selectMany(selectStatement).stream().map(EntityToVOConverter::convert).collect(Collectors.toList());


        SelectStatementProvider countStatement = builder
                .build().render(RenderingStrategies.MYBATIS3);

        long total = logRecordDao.count(countStatement);



        return composePageResult(current, size, list, total);
    }

}
